﻿using daonaite.core.Attribute;
using daonaite.Model.system.menu;
using daonaite.Model.system.roleMenu;
using SqlSugar;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace daonaite.DAL.system
{
    /// <summary>
    /// 菜单操作类
    /// </summary>
    [AutoLoad]
    public class D_menu
    {
        private readonly ISqlSugarClient client;

        public D_menu(ISqlSugarClient client)
        {
            this.client = client;
        }



        /// <summary>
        /// 获取角色菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<sys_menu>?> getrolemenuAsync(int[] roleIds)
        {
            string[]? sids = null;
            for (int i = 0; i < roleIds.Length; i++)
            {
                var tempRm = client.Queryable<sys_role_menu>().First(it => it.roleId == roleIds[i]);
                if (tempRm == null || tempRm.menuIds == null)
                {
                    break;
                }
                if (sids == null)
                {
                    sids = tempRm.menuIds.Split(",");
                }
                else
                {

                    sids = sids.Union(tempRm.menuIds.Split(",")).ToArray();
                }
            }
            if (sids == null)
            {
                return null;
            }

            object[] ids = Array.ConvertAll<string, object>(sids, s => (object)s);
            List<sys_menu> treeMenu = await client.Queryable<sys_menu>()
                .Where(x => x.deleteFlag == "N").Where(x => x.type == "M").Where(x => x.ableFlag == "Y").OrderBy(x => x.order)
                .ToTreeAsync(it => it.child, it => it.fid, 0, ids);

            return treeMenu;
        }


        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<sys_menu>?> getmenuAsync(Dto_menu data)
        {
            var expable = Expressionable.Create<sys_menu>();
            expable.And(c => c.deleteFlag == "N");
            expable.And(c => c.type == "M");
            expable.AndIF(!string.IsNullOrEmpty(data.menuName), c => c.menuName.Contains(data.menuName));
            var exp = expable.ToExpression();
            List<sys_menu> treeMenu = await client.Queryable<sys_menu>().Where(exp).ToTreeAsync(it => it.child, it => it.fid, 0);

            return treeMenu;
        }

        /// <summary>
        /// 获取菜单与按钮
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<List<sys_menu>?> getmenuandbuttonAsync()
        {
            var expable = Expressionable.Create<sys_menu>();
            expable.And(c => c.deleteFlag == "N");
            var exp = expable.ToExpression();
            List<sys_menu> treeMenu = await client.Queryable<sys_menu>().Where(exp).ToTreeAsync(it => it.child, it => it.fid, 0);

            return treeMenu;
        }

        /// <summary>
        /// 查询菜单信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<List<sys_menu>> selectAsync(Dto_menu data)
        {
            var expable = Expressionable.Create<sys_menu>();
            expable.AndIF(!string.IsNullOrEmpty(data.menuName), it => it.menuName.Contains(data.menuName));
            expable.AndIF(!string.IsNullOrEmpty(data.path), it => it.path == data.path);
            expable.AndIF(data.fid != null, it => it.fid == data.fid);
            var exp = expable.ToExpression();
            List<sys_menu> menulist = await client.Queryable<sys_menu>().Where(exp).ToListAsync();
            return menulist;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<int> add(sys_menu data)
        {
            return await client.Insertable(data).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据id获取菜单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<sys_menu> getAsync(int id)
        {
            return await client.Queryable<sys_menu>().FirstAsync(it => it.id == id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<int> updateAsync(sys_menu data)
        {
            return await client.Updateable(data).IgnoreColumns(ignoreAllNullColumns: true)
                   .WhereColumns(it => new { it.id }).ExecuteCommandAsync();
        }

        /// <summary>
        /// 查询菜单按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<sys_menu>> selectButtonAsync(int id)
        {
            var expable = Expressionable.Create<sys_menu>();
            expable.And(it => it.fid == id);
            expable.And(c => c.deleteFlag == "N");
            expable.And(c => c.type == "B");
            var exp = expable.ToExpression();
            List<sys_menu> menulist = await client.Queryable<sys_menu>().Where(exp).ToListAsync();
            return menulist;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> delete(int id)
        {
            return await client.Deleteable<sys_menu>().Where(it => it.id == id).ExecuteCommandAsync();
        }

        /// <summary>
        /// 一键添加按钮, 增删查改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task onkeyadd(int id)
        {
            sys_menu badd = new sys_menu();
            sys_menu bupdate = new sys_menu();
            sys_menu bselect = new sys_menu();
            sys_menu bdelete = new sys_menu();

            badd.fid = id;
            badd.menuName = "增加";
            badd.path = $"BTN{id}_ADD";
            badd.type = "B";
            badd.deleteFlag = "N";
            badd.ableFlag = "Y";
            await add(badd);

            bupdate.fid = id;
            bupdate.menuName = "编辑";
            bupdate.path = $"BTN{id}_UPDATE";
            bupdate.type = "B";
            bupdate.deleteFlag = "N";
            bupdate.ableFlag = "Y";
            await add(bupdate);

            bselect.fid = id;
            bselect.menuName = "查询";
            bselect.path = $"BTN{id}_SELECT";
            bselect.type = "B";
            bselect.deleteFlag = "N";
            bselect.ableFlag = "Y";
            await add(bselect);

            bdelete.fid = id;
            bdelete.menuName = "删除";
            bdelete.path = $"BTN{id}_DELETED";
            bdelete.type = "B";
            bdelete.deleteFlag = "N";
            bdelete.ableFlag = "Y";
            await add(bdelete);
        }
    }
}
